<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>开始使用 layui</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
</head>
<body style="padding-top: 100px" class="layui-bg-black">
<!-- 你的 HTML 代码 start -->

<div class="layui-container ">
    <div class="layui-row">

        <div class="layui-col-md6 layui-col-md-offset3">


            <div class="layui-card layui-bg-gray">
                <div class="layui-card-header">欢迎注册</div>
                <div class="layui-card-body">
                    <form class="layui-form" id="registForm" enctype="multipart/form-data">

                        <div class="layui-form-item">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <input id="usernamebl" type="text" name="username" required lay-verify="required"
                                       placeholder="请输入用户名" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">密码 </label>
                            <div class="layui-input-inline">
                                <input id="pass" type="password" name="password" required lay-verify="required"
                                       placeholder="请输入密码" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">确认密码 </label>
                            <div class="layui-input-inline">
                                <input id="pass1" type="password" name="password1" required lay-verify="required"
                                       placeholder="请输入密码" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">性别</label>
                            <div class="layui-input-inline">
                                <input type="radio" name="sex" value="1" title="男">
                                <input type="radio" name="sex" value="0" title="女" checked>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">生日</label>
                            <div class="layui-input-inline">
                                <input type="text" name="date" id="date" lay-verify="date" placeholder="yyyy-MM-dd"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">头像</label>
                            <div class="layui-input-inline">
                                <input type="file" name="photo">
                            </div>
                        </div>


                        <div class="layui-form-item">
                            <label class="layui-form-label">验证码 </label>
                            <div class="layui-input-inline">
                                <input type="text" name="captchar" required lay-verify="required" placeholder="请输入验证码"
                                       autocomplete="off" class="layui-input">
                            </div>

                        </div>


                        <div class="layui-form-item">
                            <label class="layui-form-label">验证码图片 </label>
                            <div class="layui-input-inline">
                                <img id="captcharimg" src="/captcha">
                            </div>

                        </div>


                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="loginForm">立即提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>

                    </form>
                </div>
            </div>


        </div>

    </div>
</div>


<!-- 你的 HTML 代码 end -->

<script src="./layui/layui.js"></script>
<script>
    layui.use(function () {
        var layer = layui.layer
            , form = layui.form;

        var $ = layui.$;  // 引入jquery的核心对象
        var un = false;
        var pa = false;
        var laydate = layui.laydate; // 声明要使用layui的日期组件

        $("#usernamebl").blur(function () {
            //获取用户名
            var username = $("#usernamebl").val();
            if (username != null && username != "") {
                $.ajax({
                    url: '/onepiece/uname?username=' + username,
                    type: 'get',
                    success: function (ret) {
                        if (ret.code != 1) {
                            layer.msg(ret.msg);
                            un = true;
                        }else {
                            un = false;
                        }
                    }
                });
            }
        });
        $("#pass1").blur(function () {
            //获取用户名
            var pass = $("#pass").val();
            var pass1 = $("#pass1").val();

            if (pass != null && pass != "" && pass1 != null && pass1 != "") {
                $.ajax({
                    url: '/onepiece/pass',
                    type: 'post',
                    data: "password=" + pass + "&password1=" + pass1,
                    success: function (ret) {
                        if (ret.code != 1) {
                            layer.msg(ret.msg);
                            pa = true;
                        }else {
                            pa = false;
                        }
                    }
                });
            }
        });

        //日期
        laydate.render({
            elem: '#date'
        });

        //监听提交
        form.on('submit(loginForm)', function (data) {

            // 获取带有文件上传的表单的数据
            var form = new FormData(document.getElementById("registForm"));
            if (un){
                layer.msg('用户名存在');
            }else if (pa){
                layer.msg('俩次密码不一致');
            }else {
                $.ajax({
                    url: '/user/regist',
                    type: 'post',
                    data: form,
                    processData: false,
                    contentType: false,
                    success: function (ret) {

                        if (ret.code == 1){
                            window.location = "/login.html";
                        }else {
                            layer.msg(ret.msg)
                        }
                    }
                });
            }
            // 发送ajax请求给后端登录接口



            // 取消表单的默认提交行为
            return false;
        });


        // 注册验证码的单击事件
        $("#captcharimg").click(function () {

            $("#captcharimg").attr('src', '/captcha?t=' + new Date().getTime());

        });

    });


    // $(function () {
    //
    // });
</script>
</body>
</html>
